﻿using System.Globalization;
using System.Windows.Controls;

namespace SqliteHelper.Classes
{
   public class TableNameValidationRule : ValidationRule
   {
      /// <summary>
      /// When overridden in a derived class, performs validation checks on a value.
      /// </summary>
      /// <param name="value">The value from the binding target to check.</param><param name="cultureInfo">The culture to use in this rule.</param>
      /// <returns>A <see cref="T:System.Windows.Controls.ValidationResult"/> object.</returns>
      public override ValidationResult Validate (object value, CultureInfo cultureInfo)
      {
         string error;
         var tableName = value as string;
         if (!string.IsNullOrEmpty (tableName))
         {
            if (tableName.Contains (" "))
               error = "Name cannot have spaces.";
            else if (char.IsLetter (tableName, 0) || tableName.StartsWith ("_"))
               return ValidationResult.ValidResult;
            else
               error = "Name must start with a letter or underscore.";
         }
         else
            error = "Name must be specified.";

         return new ValidationResult (false, error);
      }
   }
}
